//
//  CellsViewController.m
//  Cells
//
//  Created by KaiKai on 11-8-3.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CellsViewController.h"
#import "CustomCell.h"

@implementation CellsViewController

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook", @"Name", @"White", @"Color", nil];
    NSDictionary *row2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"MacBook Pro", @"Name", @"Silver", @"Color", nil];
    NSDictionary *row3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"iMac", @"Name", @"White", @"Color", nil];
    NSDictionary *row4 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mac Mini", @"Name", @"Silver", @"Color", nil];
    
    computers = [[NSArray alloc] initWithObjects:row1, row2, row3, row4, nil];
    [row1 release];
    [row2 release];
    [row3 release];
    [row4 release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [computers release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [computers count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *CellTableIdentifier = @"CellTableIdentifier";
//    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
//    if (cell == nil)
//    {
//        CGRect cellFrame = CGRectMake(0, 0, 300, 65);
//        cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:CellTableIdentifier] autorelease];
//        
//        CGRect nameLabelRect = CGRectMake(0, 5, 70, 15);
//        UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
//        nameLabel.textAlignment = UITextAlignmentRight;
//        nameLabel.text = @"Name:";
//        nameLabel.font = [UIFont boldSystemFontOfSize:12];
//        [cell.contentView addSubview:nameLabel];
//        [nameLabel release];
//        
//        CGRect colorLabelRect = CGRectMake(0, 26, 70, 15);
//        UILabel *colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];
//        colorLabel.textAlignment = UITextAlignmentRight;
//        colorLabel.text = @"Color:";
//        colorLabel.font = [UIFont boldSystemFontOfSize:12];
//        [cell.contentView addSubview:colorLabel];
//        [colorLabel release];
//        
//        CGRect nameValueRect = CGRectMake(80, 5, 200, 15);
//        UILabel *nameValue = [[UILabel alloc] initWithFrame:nameValueRect];
//        nameValue.tag = 1;
//        [cell.contentView addSubview:nameValue];
//        [nameValue release];
//        
//        CGRect colorValueRect = CGRectMake(80, 25, 200, 15);
//        UILabel *colorValue = [[UILabel alloc] initWithFrame:colorValueRect];
//        colorValue.tag = 2;
//        [cell.contentView addSubview:colorValue];
//        [colorValue release];
//    }
//    
//    NSUInteger row = [indexPath row];
//    NSDictionary *rowData = [computers objectAtIndex:row];
//    UILabel *name = (UILabel *)[cell.contentView viewWithTag:1];
//    name.text = [rowData objectForKey:@"Name"];
//    
//    UILabel *color = (UILabel *)[cell.contentView viewWithTag:2];
//    color.text = [rowData objectForKey:@"Color"];
//    
//    return cell;


    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    NSUInteger row = [indexPath row];
    NSDictionary *rowData =  [computers objectAtIndex:row];
    cell.colorLabel.text = [rowData objectForKey:@"Color"];
    cell.nameLabel.text = [rowData objectForKey:@"Name"];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 66;
}

@end
